/* eslint-disable no-underscore-dangle */
module.exports = (app) => {
  const initMiddleware = require('./middleware');
  const initUserRoutes = require('./user');
  const initOrganizationRoutes = require('./organization');
  const initCloudApiRoutes = require('./cloudapi');
  const initCallback = require('./callback');
  const initContract = require('./contract');
  const initWorker = require('./worker');
  const initTenant = require('./tenant');
  const initSeal = require('./seal');
  const initSend = require('./send');
  const initTemplate = require('./template');
  const initSettings = require('./settings');
  const initExternal = require('./external');
  // eslint-disable-next-line no-underscore-dangle
  const routeStartIndex = app._router.stack.length;
  initMiddleware(app);
  initUserRoutes(app);
  initCloudApiRoutes(app);
  initCallback(app);
  initContract(app);
  initWorker(app);
  initTenant(app);
  initOrganizationRoutes(app);
  initSeal(app);
  initSend(app);
  initTemplate(app);
  initSettings(app);
  initExternal(app);
  const routesLength = app._router.stack.length - routeStartIndex;
  console.log('routeStartIndex, routesLength', routeStartIndex, routesLength);

  return () => {
    // 卸载以上注册的router
    app._router.stack.splice(routeStartIndex, routesLength);
  };
};
